{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'example.html'"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#基于plotly的动态数据可视化\n",
    "import plotly as py\n",
    "import plotly.graph_objects as go\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "pyplt=py.offline.plot\n",
    "\n",
    "url=\"dat/NCP.csv\"\n",
    "df=pd.read_csv(url)\n",
    "\n",
    "date=pd.date_range(start='22/1/2020',end='11/2/2020')\n",
    "\n",
    "figure= go.Figure()\n",
    "\n",
    "# 对绘图添加 traces\n",
    "trace1 = go.Scatter(x=date, y=df.confirmedCount,\n",
    "                    marker=dict(color='#FFC125'),\n",
    "                    mode = 'lines+markers',\n",
    "                    name='确诊',\n",
    "                    xaxis='x2', yaxis='y2')\n",
    "trace2 = go.Scatter(x=date, y=df.suspectedCount,\n",
    "                    marker=dict(color='#FF0000'),\n",
    "                    name='疑似',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x2', yaxis='y2')\n",
    "trace3 = go.Scatter(x=date, y=df.curedCount,\n",
    "                    marker=dict(color='#ADFF2F'),\n",
    "                    name='治愈',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x2', yaxis='y2')\n",
    "trace4 = go.Scatter(x=date, y=df.deadCount,\n",
    "                    marker=dict(color='#1E1E1E'),\n",
    "                    name='死亡',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x2', yaxis='y2')\n",
    "trace5 = go.Scatter(x=date, y=df.confirmedRise,\n",
    "                    marker=dict(color='#FFC125'),\n",
    "                    mode = 'lines+markers',\n",
    "                    name='新增确诊',\n",
    "                    xaxis='x', yaxis='y')\n",
    "trace6 = go.Scatter(x=date, y=df.suspectedRise,\n",
    "                    marker=dict(color='#FF0000'),\n",
    "                    name='新增疑似',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x', yaxis='y')\n",
    "trace7 = go.Scatter(x=date, y=df.curedRise,\n",
    "                    marker=dict(color='#ADFF2F'),\n",
    "                    name='新增治愈',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x', yaxis='y')\n",
    "trace8 = go.Scatter(x=date, y=df.deadRise,\n",
    "                    marker=dict(color='#1E1E1E'),\n",
    "                    name='新增死亡',\n",
    "                    mode = 'lines+markers',\n",
    "                    xaxis='x', yaxis='y')\n",
    "\n",
    "figure.add_traces([trace1, trace2,trace3,trace4])\n",
    "figure.add_traces([trace5, trace6,trace7,trace8])\n",
    "figure['layout']['xaxis2'] = {}\n",
    "figure['layout']['yaxis2'] = {}\n",
    "\n",
    "figure.layout.yaxis.update({'domain': [0, .45]})\n",
    "figure.layout.yaxis2.update({'domain': [.6, 1.]})\n",
    "\n",
    "figure.layout.yaxis2.update({'title': '人数'})\n",
    "figure.layout.yaxis.update({'title': '人数'})\n",
    "figure.layout.xaxis2.update({'title': '日期'})\n",
    "figure.layout.xaxis.update({'title': '日期'})\n",
    "figure.layout.update({'height':800})\n",
    "figure.update_layout(\n",
    "    title_text=\"疫情趋势图\",\n",
    "   \n",
    ")\n",
    "\n",
    "figure.update_layout(\n",
    "\n",
    "    hovermode=\"x\",\n",
    "    template=\"plotly_white\",\n",
    ")\n",
    "\n",
    "\n",
    "\n",
    "pyplt(figure, filename='example.html', show_link=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
